package server.business.play.script;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import server.ScriptEnum;
import server.ServerTransportProxy;
import server.business.play.PlayTransport;
import server.business.poker.util.PokerCheck;
import server.commonData.Player;
import server.commonData.PokerPlayer;
import server.commonData.Room;
import server.commonData.Table;
import server.commonData.User;
import server.messageId.S2CMessageId;
import server.msgBean.PlayMessage.PlayResultRes;
import server.msgBean.PlayMessage.PlayerResultPokerInfo;

import com.core.initialize.Message;
/**
 * 选牌状态
 * @author King
 *
 */
public class PlayChooseScript implements IPlayGameScript
{
	@Override
	public int getId() {
		return ScriptEnum.PLAY_CHOOSE_POKER;
	}

	@Override
	public void onUpdate(Table table, long curMill) {
		Map<Long, PokerPlayer> players = table.getPlayingPlayers();
		//自动给机器人自动选牌
		for(PokerPlayer player:players.values())
		{
			if(player.getPokerData().isChoose())
				continue;
			int pokerType =	PokerCheck.getNiuType(player.getPokerData().getOwnPoker());
			player.getPokerData().setPokerType(pokerType);
		}
		
		if(curMill<table.getXuanpaiEndtime())
			return;
		//基础倍率
		int jichuBeilv = table.getRoom().getBeilv();
		
		//给没有选派的玩家自动选牌
		for(PokerPlayer player:players.values())
		{
			if(player.getPokerData().isChoose())
			{
				//没有选牌则设置为没牛 可以扩展
				player.getPokerData().setPokerType(PokerCheck.MEINIU);
			}
			//计算倍率和最大颜色 最大值
			player.getPokerData().setBeilv(PokerCheck.getPeilv(player.getPokerData().getPokerType()));
			player.getPokerData().setMaxPokerColor(PokerCheck.getMaxValColor(player.getPokerData().getOwnPoker()));
			player.getPokerData().setMaxPokerVal(PokerCheck.getMaxVal(player.getPokerData().getOwnPoker()));
		}
		
		//结果返回
		PlayResultRes.Builder builder = PlayResultRes.newBuilder();
		Message msg = Message.newMessage();
		msg.setModuleAction(S2CMessageId.PLAY.MODULE_ID, S2CMessageId.PLAY.RESULT_RES);
		
		
		//计算金币 结果发给客户端 
		PokerPlayer zhuangPlayer = players.get(table.getZhuangPlayerId());
		boolean isZhuangWin = false;
		for(PokerPlayer player:players.values())
		{
			if(player.getPlayer().getId()==table.getZhuangPlayerId())
				continue;
			PlayerResultPokerInfo.Builder oneBuilder = PlayerResultPokerInfo.newBuilder();
			oneBuilder.setUserId(player.getPlayer().getId());
			
			//如果庄家跑了则默认给1倍倍率金币
			if(zhuangPlayer==null)
			{
				player.getPlayer().addMoney(jichuBeilv);
				player.getPlayer().addWinNum();
				
				oneBuilder.setNowGold(player.getPlayer().getMoney());
				builder.addResultList(oneBuilder);
				continue;
			}
			//判断输赢
			if(PokerCheck.isZhuangJiaWin(zhuangPlayer.getPokerData(), player.getPokerData()))
			{
				isZhuangWin = true;
				player.getPlayer().addFailNum();
				int delMoney = player.getPlayer().delMoney(zhuangPlayer.getPokerData().getBeilv()*jichuBeilv);
				zhuangPlayer.getPlayer().addMoney(delMoney);
				oneBuilder.setNowGold(player.getPlayer().getMoney());
			}
			else
			{
				player.getPlayer().addWinNum();
				int delMoney = zhuangPlayer.getPlayer().delMoney(player.getPokerData().getBeilv()*jichuBeilv);
				
				player.getPlayer().addMoney(delMoney);
				oneBuilder.setNowGold(player.getPlayer().getMoney());
			}
			builder.addResultList(oneBuilder);
		}
		if(zhuangPlayer!=null)
		{
			if(isZhuangWin)
				zhuangPlayer.getPlayer().addWinNum();
			else
				zhuangPlayer.getPlayer().addFailNum();
			//把庄家加入记录
			PlayerResultPokerInfo.Builder oneBuilder = PlayerResultPokerInfo.newBuilder();
			oneBuilder.setUserId(zhuangPlayer.getPlayer().getId());
			oneBuilder.setNowGold(zhuangPlayer.getPlayer().getMoney());
			builder.addResultList(oneBuilder);
		}
		msg.setBody(builder);
		//收集在线的玩家集合
		List<User> userList = new ArrayList<User>();
		for(PokerPlayer player:players.values())
		{
			if(player.getPlayer().getType()!=Player.USER)
				continue;
			userList.add((User)player.getPlayer());
		}

		ServerTransportProxy.sendMsgByUser(msg, userList);
		
		//检测玩家金币是否达到最低需求   没达到则踢出玩家
		List<User> removeList = new ArrayList<User>();
		for(PokerPlayer player:players.values())
		{
			if(player.getPlayer().getMoney()<table.getRoom().getBeilv())
			{
				if(player.getPlayer().getType()==Player.ROBOT){
					//给机器人自动增加1000金币
					player.getPlayer().addMoney(1000);
					continue;
				}
				removeList.add((User)player.getPlayer());
			}
		}
		if(!removeList.isEmpty())
		{
			for(User user:removeList)
			{
				table.getRoom().leftTable(user);
			}
			PlayTransport.kickToRoomList(removeList);
		}
		
		//一局结束 进入等待状态
		for(PokerPlayer player:players.values())
		{
			player.setPokerData(null);
			player.setPlay(false);
		}
		table.setXuanpaiEndtime(0);
		table.setZunbeiEndtime(System.currentTimeMillis()+10000);
		table.setStatus(ScriptEnum.PLAY_WAIT);	
	}
}
